home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Documentation / develop / develop Issue 14 / develop 14 code / Derived Media Handlers / MakeMyMediaMovies / MakeMyMediaMovies.c next >
Encoding:
Text File  |  1993-03-12  |  11.3 KB  |  484 lines  |  [TEXT/KAHL]

  1. //--------------------------------------------------------------------------
  2. //
  3. //        MakeMyMediaMovies.
  4. //            by John Wang
  5. //
  6. //        Description:    Create movies for QuickDraw media.
  7. //
  8. //        Version:    1.0        02/25/93    New.
  9. //
  10. //--------------------------------------------------------------------------
  11.  
  12. //
  13. //    #defines
  14. //
  15.  
  16. #define    APPLEID                    128            
  17. #define    ABOUTMECOMMAND            1
  18.  
  19. #define    CREATEID                129
  20. #define    CREATE_ROWS             1
  21. #define CREATE_LINES            2
  22. #define CREATE_BOXES            3
  23. #define CREATE_BALL                4
  24. #define    FILE_QUIT                 6
  25.  
  26. #define    MAXWINDOWS                5
  27.  
  28. //
  29. //    #includes
  30. //
  31.  
  32. #include <GestaltEqu.h>
  33. #include <Movies.h>
  34. #include "QDrawHandler.h"
  35.  
  36. //
  37. //    Global Variables.
  38. //
  39.  
  40. Boolean                DoneFlag;
  41. WindowPtr            MyWindow;
  42.  
  43. //--------------------------------------------------------------------------
  44.  
  45. waitClick()
  46. {
  47.     do {
  48.     } while (!Button());
  49.     do {
  50.     } while (Button());
  51. }
  52.  
  53. //--------------------------------------------------------------------------
  54.  
  55. void doRows(Media myMedia, short myWidth, short myHeight, Rect *windRect)
  56. {
  57.     long                    i;
  58.     Rect                    drawRect;
  59.     RGBColor                myColor;
  60.     PicHandle                myPict;
  61.     QDrawDescriptionHandle    myQDDesc;
  62.  
  63.     //    Add samples to media.
  64.     myQDDesc = (QDrawDescriptionHandle) NewHandle(sizeof(QDrawDescription));
  65.     (**myQDDesc).size = sizeof(QDrawDescription);
  66.     (**myQDDesc).type = QDrawMediaType;
  67.     (**myQDDesc).version = QDMediaVersion;
  68.     SetRect(&drawRect, 0, 0, 500, 80);
  69.     for (i=0; i<10; i++) {
  70.         short    mySync;
  71.         
  72.         myPict = OpenPicture(windRect);
  73.         if (i==0) {
  74.             EraseRect(windRect);
  75.             mySync = 0;
  76.         } else {
  77.             mySync = mediaSampleNotSync;
  78.         }
  79.         myColor.red = myColor.green = myColor.blue = (i*25) << 8;
  80.         RGBForeColor(&myColor);
  81.         PaintRect(&drawRect);
  82.         ClosePicture();
  83.         if (AddGraphics(myMedia, myPict, myQDDesc, 600, mySync, nil)) {
  84.             DebugStr("\PAddMediaSample failed.");
  85.             ExitToShell();
  86.         }
  87.         DrawPicture(myPict, windRect);
  88.         KillPicture(myPict);
  89.         OffsetRect(&drawRect, 0, 40);    
  90.     }
  91. }
  92.  
  93. //--------------------------------------------------------------------------
  94.  
  95. void doLines(Media myMedia, short myWidth, short myHeight, Rect *windRect)
  96. {
  97.     long                    i;
  98.     Rect                    drawRect;
  99.     RGBColor                myColor;
  100.     PicHandle                myPict;
  101.     QDrawDescriptionHandle    myQDDesc;
  102.     
  103.     //    Add samples to media.
  104.     if (BeginMediaEdits(myMedia)) {
  105.         DebugStr("\PBeginMediaEdits failed.");
  106.         ExitToShell();
  107.     }
  108.     myQDDesc = (QDrawDescriptionHandle) NewHandle(sizeof(QDrawDescription));
  109.     (**myQDDesc).size = sizeof(QDrawDescription);
  110.     (**myQDDesc).type = QDrawMediaType;
  111.     (**myQDDesc).version = QDMediaVersion;
  112.     SetRect(&drawRect, 0, 0, myWidth, myHeight);
  113.     for (i=0; i<myWidth; i++) {
  114.         short    mySync;
  115.  
  116.         myPict = OpenPicture(windRect);
  117.         myColor.red = (i & 0xff) << 8;
  118.         myColor.green = ((255 - i) & 0xff) << 8;
  119.         myColor.blue = ((128 - i) & 0xff) << 8;
  120.         if (i==0) {
  121.             EraseRect(windRect);
  122.             mySync = 0;
  123.         } else {
  124.             mySync = mediaSampleNotSync;
  125.         }
  126.         RGBForeColor(&myColor);
  127.         MoveTo(i,0);
  128.         LineTo(i,myHeight);
  129.         ClosePicture();
  130.         DrawPicture(myPict, windRect);
  131.         if (AddGraphics(myMedia, myPict, myQDDesc, 20, mySync, nil)) {
  132.             DebugStr("\PAddMediaSample failed.");
  133.             ExitToShell();
  134.         }
  135.         KillPicture(myPict);
  136.     }
  137. }
  138.  
  139. //--------------------------------------------------------------------------
  140.  
  141. void doBoxes(Media myMedia, short myWidth, short myHeight, Rect *windRect)
  142. {
  143.     long                    i;
  144.     Rect                    drawRect;
  145.     RGBColor                myColor;
  146.     PicHandle                myPict;
  147.     QDrawDescriptionHandle    myQDDesc;
  148.     
  149.     //    Add samples to media.
  150.     if (BeginMediaEdits(myMedia)) {
  151.         DebugStr("\PBeginMediaEdits failed.");
  152.         ExitToShell();
  153.     }
  154.     myQDDesc = (QDrawDescriptionHandle) NewHandle(sizeof(QDrawDescription));
  155.     (**myQDDesc).size = sizeof(QDrawDescription);
  156.     (**myQDDesc).type = QDrawMediaType;
  157.     (**myQDDesc).version = QDMediaVersion;
  158.     SetRect(&drawRect, 0, 0, myWidth, myHeight);
  159.     for (i=0; i<50; i++) {
  160.         short    mySync;
  161.  
  162.         myPict = OpenPicture(windRect);
  163.         myColor.red = ((i*10) & 0xFF) << 8;
  164.         myColor.green = ((i*20) & 0xFF) << 8;
  165.         myColor.blue = ((i*30) & 0xFF) << 8;
  166.         RGBForeColor(&myColor);
  167.         if (i==0) {
  168.             EraseRect(windRect);
  169.             mySync = 0;
  170.         } else {
  171.             mySync = mediaSampleNotSync;
  172.         }
  173.         PaintRect(&drawRect);
  174.         ClosePicture();
  175.         DrawPicture(myPict, windRect);
  176.         InsetRect(&drawRect, myWidth / 100, myHeight / 100);    
  177.         if (AddGraphics(myMedia, myPict, myQDDesc, 40, mySync, nil)) {
  178.             DebugStr("\PAddMediaSample failed.");
  179.             ExitToShell();
  180.         }
  181.         KillPicture(myPict);
  182.     }
  183. }
  184.  
  185. //--------------------------------------------------------------------------
  186.  
  187. void doBall(Media myMedia, short myWidth, short myHeight, Rect *windRect)
  188. {
  189.     long                    i;
  190.     Rect                    drawRect;
  191.     RGBColor                myColor;
  192.     PicHandle                myPict;
  193.     QDrawDescriptionHandle    myQDDesc;
  194.     short                    lx, ly, x, y, vx, vy;
  195.     
  196.     //    Add samples to media.
  197.     if (BeginMediaEdits(myMedia)) {
  198.         DebugStr("\PBeginMediaEdits failed.");
  199.         ExitToShell();
  200.     }
  201.     myQDDesc = (QDrawDescriptionHandle) NewHandle(sizeof(QDrawDescription));
  202.     (**myQDDesc).size = sizeof(QDrawDescription);
  203.     (**myQDDesc).type = QDrawMediaType;
  204.     (**myQDDesc).version = QDMediaVersion;
  205.     SetRect(&drawRect, 0, 0, 30, 30);
  206.     EraseRect(windRect);
  207.     lx = ly = x = y = 10;
  208.     vx = 5, vy = 3;
  209.     for (i=0; i<1000; i++) {
  210.         short    mySync;
  211.  
  212.         myPict = OpenPicture(windRect);
  213.         if ((i % 10) == 0) {
  214.             mySync = 0;
  215.             myColor.red = ((i*15) & 0xFF) << 8;
  216.             myColor.green = ((i*10) & 0xFF) << 8;
  217.             myColor.blue = ((i*5) & 0xFF) << 8;
  218.             RGBBackColor(&myColor);
  219.             EraseRect(windRect);
  220.         } else {
  221.             mySync = mediaSampleNotSync;
  222.         }
  223.         myColor.red = ((i*5) & 0xFF) << 8;
  224.         myColor.green = ((i*10) & 0xFF) << 8;
  225.         myColor.blue = ((i*15) & 0xFF) << 8;
  226.         RGBForeColor(&myColor);
  227.         SetRect(&drawRect, lx-15, ly-15, lx+15, ly+15);
  228.         EraseOval(&drawRect);
  229.         SetRect(&drawRect, x-15, y-15, x+15, y+15);
  230.         PaintOval(&drawRect);
  231.         lx = x;
  232.         ly = y;
  233.         x += vx;
  234.         y += vy;
  235.         if (x > myWidth) {
  236.             x = myWidth;
  237.             vx = -vx;
  238.         } else if (x < 0) {
  239.             x = 0;
  240.             vx = -vx;
  241.         }
  242.         if (y > myHeight) {
  243.             y = myHeight;
  244.             vy = -vy;
  245.         } else if (y < 0) {
  246.             y = 0;
  247.             vy = -vy;
  248.         }
  249.         ClosePicture();
  250.         DrawPicture(myPict, windRect);
  251.         if (AddGraphics(myMedia, myPict, myQDDesc, 40, mySync, nil)) {
  252.             DebugStr("\PAddMediaSample failed.");
  253.             ExitToShell();
  254.         }
  255.         KillPicture(myPict);
  256.     }
  257. }
  258.  
  259. //--------------------------------------------------------------------------
  260.  
  261. void makeMovie(ProcPtr myFunc(Media myMedia, short myWidth, short myHeight, Rect *windRect), short myWidth, short myHeight)
  262. {
  263.     OSErr                    err;
  264.     Rect                    windRect;
  265.     StandardFileReply        reply;
  266.     short                    resRefNum;
  267.     Movie                    myMovie;
  268.     Track                    myTrack;
  269.     Media                    myMedia;
  270.     
  271.     //    Set window size and rect.
  272.     SizeWindow(MyWindow, myWidth, myHeight, FALSE);
  273.     SetRect(&windRect, 0, 0, myWidth, myHeight);
  274.     EraseRect(&windRect);
  275.     
  276.     //    Create file.
  277.     StandardPutFile("\PName of movie file to create:", "\PUntitled", &reply);
  278.     if (!reply.sfGood)
  279.         ExitToShell();
  280.     if (CreateMovieFile(&reply.sfFile, 'TVOD', 0,
  281.                     createMovieFileDeleteCurFile, &resRefNum, &myMovie)) {
  282.         DebugStr("\PCreateMovieFile failed.");
  283.         ExitToShell();
  284.     }
  285.     
  286.     //    Create track and media.
  287.     if ((myTrack = NewMovieTrack(myMovie, (long) myWidth << 16, (long) myHeight << 16, 0)) == nil) {
  288.         DebugStr("\PNewMovieTrack failed.");
  289.         ExitToShell();
  290.     }
  291.     if ((myMedia = NewTrackMedia(myTrack, QDrawMediaType, 600, nil, (OSType) nil)) == nil) {
  292.         DebugStr("\PNewTrackMedia failed. Is the QuickDraw Media handler installed?");
  293.         ExitToShell();
  294.     }
  295.     
  296.     //    Start editing session.
  297.     if (BeginMediaEdits(myMedia)) {
  298.         DebugStr("\PBeginMediaEdits failed.");
  299.         ExitToShell();
  300.     }
  301.     
  302.     //    Call function to add media
  303.     (myFunc) (myMedia, myWidth, myHeight, &windRect);
  304.  
  305.     //    Place media into movie and save movie.
  306.     if (EndMediaEdits(myMedia)) {
  307.         DebugStr("\PEndMediaEdits failed.");
  308.         ExitToShell();
  309.     }
  310.     if (InsertMediaIntoTrack(myTrack, 0, 0, GetMediaDuration(myMedia), kFix1)) {
  311.         DebugStr("\PInsertMediaIntoTrack failed.");
  312.         ExitToShell();
  313.     }
  314.  
  315.     //    Save movie and close file.
  316.     if (AddMovieResource( myMovie, resRefNum, nil, nil)) {
  317.         DebugStr("\PAddMovieResource failed.");
  318.         ExitToShell();
  319.     }
  320.     if (CloseMovieFile(resRefNum)) {
  321.         DebugStr("\PCloseMovieFile failed.");
  322.         ExitToShell();
  323.     }
  324.     DisposeMovie(myMovie);
  325. }
  326.  
  327. //--------------------------------------------------------------------------
  328.  
  329. void init()
  330. {
  331.     OSErr                err;
  332.     long                QDfeature, QTfeature;
  333.     Handle                myHandle;
  334.     Rect                myRect;
  335.     
  336.     //    Initialize Managaer.
  337.     MaxApplZone();
  338.     MoreMasters();
  339.     MoreMasters();
  340.     MoreMasters();
  341.     MoreMasters();    InitGraf(&qd.thePort);
  342.     FlushEvents(everyEvent, 0);
  343.     InitWindows();
  344.     InitDialogs(nil);
  345.     InitCursor();
  346.  
  347.     //    Use Gestalt to find if QuickTime is avaiable.
  348.     //    Use Gestalt to find out version of ColorSync available
  349.     if (Gestalt(gestaltQuickdrawVersion, &QDfeature))
  350.         ExitToShell();
  351.     if (QDfeature < 0x0230)
  352.         ExitToShell();
  353.     if (Gestalt(gestaltQuickTime, &QTfeature))
  354.         QTfeature = 0;
  355.     if ((QTfeature >> 16) < 0x0150)
  356.         ExitToShell();
  357.  
  358.     //    Set up menus.
  359.     myHandle = GetNewMBar(128);
  360.     SetMenuBar(myHandle);
  361.     AddResMenu(GetMHandle(APPLEID), 'DRVR');
  362.     DrawMenuBar();
  363.  
  364.     //    Initialize global variables.
  365.     DoneFlag = FALSE;
  366.  
  367.     //    Open QuickTime last.
  368.     if (EnterMovies())
  369.         ExitToShell();
  370.  
  371.     //    Define output window.
  372.     SetRect(&myRect, 10, 40, 510, 440);
  373.     MyWindow = NewCWindow(nil, &myRect, "\pDisplay graphics", true, documentProc, (WindowPtr) -1, false, 0);
  374.     SetPort(MyWindow);
  375. }
  376.  
  377. //--------------------------------------------------------------------------
  378.  
  379. void doCommand(mResult)
  380.     long    mResult;
  381. {
  382.     int                     theMenu, theItem;
  383.     char                    daName[256];
  384.     GrafPtr                 savePort;
  385.  
  386.     theItem = LoWord(mResult);
  387.     theMenu = HiWord(mResult);
  388.     
  389.     switch (theMenu) {
  390.         case APPLEID:
  391.             if (theItem == ABOUTMECOMMAND) {
  392.                 ParamText("\PMakeMyMediaMovies", "\PThis program uses the 'Qdrw' derived media handler to make movies.",
  393.                             nil, nil);
  394.                 Alert(128, nil);
  395.             } else {
  396.                 GetItem(GetMHandle(APPLEID), theItem, daName);
  397.                 GetPort(&savePort);
  398.                 (void) OpenDeskAcc(daName);
  399.                 SetPort(savePort);
  400.             }
  401.             break;
  402.  
  403.         case CREATEID:
  404.             switch (theItem) {
  405.                 case CREATE_ROWS:
  406.                     makeMovie((ProcPtr) doRows, 500, 400);
  407.                     break;
  408.                 case CREATE_LINES:
  409.                     makeMovie((ProcPtr) doLines, 256, 200);
  410.                     break;
  411.                 case CREATE_BOXES:
  412.                     makeMovie((ProcPtr) doBoxes, 300, 200);
  413.                     break;
  414.                 case CREATE_BALL:
  415.                     makeMovie((ProcPtr) doBall, 500, 400);
  416.                     break;
  417.                 case FILE_QUIT:
  418.                     DoneFlag = TRUE;
  419.                     break;
  420.                 default:
  421.                     break;
  422.             }
  423.             break;
  424.     }
  425.     HiliteMenu(0);
  426.     return;
  427. }
  428.  
  429. //--------------------------------------------------------------------------
  430.  
  431. main()
  432. {
  433.     int                i;
  434.     EventRecord     myEvent;
  435.     WindowPtr        whichWindow;
  436.  
  437.     init();
  438.     for ( ;; ) {
  439.     
  440.         //    We can't just do ExitToShell because we must call ExitMovies.
  441.         if (DoneFlag)
  442.             ExitToShell();
  443.             
  444.         if (WaitNextEvent(everyEvent, &myEvent, 0, nil)) {
  445.             switch (myEvent.what) {
  446.                 case mouseDown:
  447.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  448.                         case inSysWindow:
  449.                             SystemClick(&myEvent, whichWindow);
  450.                             break;
  451.                         case inMenuBar:
  452.                             doCommand(MenuSelect(myEvent.where));
  453.                             break;
  454.                         case inContent:
  455.                             break;
  456.                         case inDrag:
  457.                             SelectWindow(whichWindow);
  458.                             DragWindow (whichWindow, myEvent.where, &qd.screenBits.bounds);
  459.                             break;
  460.                         case inGoAway:
  461.                         case inGrow:
  462.                         case inZoomIn:
  463.                         case inZoomOut:
  464.                         default:
  465.                             break;
  466.                     }
  467.                     break;
  468.                 case keyDown:
  469.                 case autoKey:
  470.                     if ( myEvent.modifiers & cmdKey )
  471.                         if ( myEvent.what == keyDown )
  472.                             doCommand(MenuKey(myEvent.message & charCodeMask));
  473.                     break;
  474.                 case updateEvt:
  475.                     break;
  476.                 case diskEvt:
  477.                 case activateEvt:
  478.                 case app4Evt:
  479.                 default:
  480.                     break;
  481.             }
  482.         }
  483.     }
  484. }